package mipo.top.admin.controller;

import mipo.top.admin.domain.Shop;
import mipo.top.admin.domain.ShopAuditLog;
import mipo.top.admin.query.ShopQuery;
import mipo.top.admin.service.IShopService;
import mipo.top.base.util.ExcelUtil;
import mipo.top.base.util.JsonResult;
import mipo.top.base.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private IShopService shopService;

    @GetMapping
    public List<Shop> sad(){
        return shopService.findall();
    }

    @PutMapping
    public JsonResult su(@RequestBody Shop shop){
        try {
            if(shop.getId()==null){
                shopService.save(shop);
            }else{
                shopService.update(shop);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("错误数据");
        }
    }
    @GetMapping("/{id}")
    public Shop wdas(@PathVariable("id") Long id){
        System.out.println(id);
        return shopService.findone(id);
    }

    @DeleteMapping("/{id}")
    public void adw(@PathVariable("id") Long id){
        shopService.remove(id);
    }

    @PostMapping
    public PageUtil<Shop> asp(@RequestBody ShopQuery shopQuery){
        return shopService.queryData(shopQuery);
    }
    @PatchMapping
    private void bacthDel(@RequestBody List<Long> ids){
        shopService.patchremove(ids);
    }
    @PostMapping("/settlement")
    public JsonResult dswqe(@RequestBody Shop shop){
        try {
            shopService.joinShop(shop);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }
    }
    @PostMapping("/settlement/again")
    public JsonResult dsw3e(@RequestBody Shop shop){
        try {
            shopService.update(shop);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }
    }
    @PostMapping("/audit/pass")
    public JsonResult sad2(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.handpass(shopAuditLog);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }
    }
    @PostMapping("/audit/reject")
    public JsonResult res(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.handreject(shopAuditLog);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }
    }
    @GetMapping("/export/excel")
    public void export(HttpServletResponse response){
        List<Shop> findall = shopService.findall();
        ExcelUtil.exportExcel(findall,"","店鋪信息",Shop.class,"店鋪信息.xlsx",response);
    }

    @PostMapping("/import/excel")
    public void imports(@RequestPart("file") MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 2, Shop.class);
        shops.forEach(System.out::println);
//        for (Shop shop : shops) {
//            shopService.save(shop);
//        }
    }
}
